package com.gcb.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 * @author Guvivi
 * Create 2010-12-14����10:35:44
 */
public class ConnOracle {
	
	private static Connection conn;
	private PreparedStatement pstmt;
	private CallableStatement cstmt;
	private DataSource ds;
	private String driverClassName;
	private String url;
	private String username;
	private String password;
	
	/**
	 * @param pstmt
	 * @param ds
	 * @param driverClassName
	 * @param url
	 * @param username
	 * @param password
	 */
	public ConnOracle(String driverClassName, String url, String username, String password) {
		super();
		this.driverClassName = driverClassName;
		this.url = url;
		this.username = username;
		this.password = password;
	}

	/**
	 * 
	 */
	public ConnOracle() {
		// TODO Auto-generated constructor stub
		this.getProperties();
		this.getConnection();
	}
	
	public ConnOracle(DataSource ds){
		try {
			conn = ds.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void getConnection(){
		try {
			Class.forName(driverClassName);
			conn = DriverManager.getConnection(url, username, password);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public PreparedStatement getPstmt(String sql){
		try {
			pstmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return pstmt;
	}


	/**
	 * 
	 * Creat 2011-6-15
	 */
	public CallableStatement getCstmt(String sql){
		try {
			cstmt = conn.prepareCall(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return cstmt;
	}
	
	/**
	 * 
	 * @param sql
	 * @return
	 */
	public ResultSet query(String sql){
		ResultSet rs = null;
		try {
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs;
	}
	public void close(){
		if(conn != null){
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void getProperties(){
		Properties pro = new Properties();
		InputStream is = ConnOracle.class.getResourceAsStream("/oracle.properties");
		try {
			pro.load(is);
			driverClassName = pro.getProperty("driverClassName");
			url = pro.getProperty("url");
			username = pro.getProperty("username");
			password = pro.getProperty("password");
			if(driverClassName == null||url == null||username == null||password == null){
				throw new Exception("读取配置文件“oracle.properties”出错");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		ConnOracle con = new ConnOracle();
	}
	
	
}
